﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Carmall.Entity;
using Carmall.BLL;

namespace Carmall.Web.Controllers
{
    public class SystemController : Controller
    {
        //
        // GET: /System/

        public ActionResult Index()
        {
            return View();
        }

        #region News
        public ActionResult CreateNews()
        {
            return View();
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateNews(FormCollection form)
        {
            var news = new NewsInfo();

            TryUpdateModel(news, new string[] { 
                "Title",
                "Content"
            }, form.ToValueProvider());
            if (String.IsNullOrEmpty(news.Title))
            {
                ViewData.ModelState.AddModelError("Title", "请填定新闻标题");
            }
            if (String.IsNullOrEmpty(news.Content))
            {
                ViewData.ModelState.AddModelError("Content", "请填写新闻内容");
            }

            news.NewsCategory = -1;
            news.Created = DateTime.Now;
            news.Display = 0;
            if (ModelState.IsValid)
            {
                BLLFactory<News>.Instance.Insert(news);
                ModelState.AddModelError("", "发布成功!");
            }
            return View();
        }

        #endregion

        #region 产品类别
        public ActionResult CategoryList()
        {
            List<CategoryInfo> list = BLLFactory<Category>.Instance.GetAll();
            return View(list);
        }

        public ActionResult AddCategory()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddCategory(FormCollection form)
        {
            var cat = new CategoryInfo();
            cat.CategoryName = "";
            TryUpdateModel(cat, new string[] { 
                "Category",
                "ParentID",
                "Notes"
            }, form.ToValueProvider());
            if (string.IsNullOrEmpty(cat.Category))
                ModelState.AddModelError("Category", "请填写类别名称");
            if (ModelState.IsValid)
            {
                BLLFactory<Category>.Instance.Insert(cat);
                return RedirectToAction("CategoryList");
            }
            return View();
        }

        public ActionResult EditCategory(int id)
        {
            CategoryInfo cat = BLLFactory<Category>.Instance.FindByID(id.ToString());
            ViewData.Model = cat;
            return View();
        }

        [HttpPost]
        public ActionResult EditCategory(int id, FormCollection form)
        {
            var cat = BLLFactory<Category>.Instance.FindByID(id.ToString());
            TryUpdateModel(cat, new string[] { 
                "Category",
                "ParentID",
                "Notes"
            }, form.ToValueProvider());
            if (string.IsNullOrEmpty(cat.Category))
                ModelState.AddModelError("Category", "请填写类别名称");
            if (ModelState.IsValid)
            {
                BLLFactory<Category>.Instance.Insert(cat);
                return RedirectToAction("CategoryList");
            }
            return RedirectToAction("CategoryList");
        }

        public ActionResult DelCategory(int id)
        {
            BLLFactory<Category>.Instance.Delete(id.ToString());
            return RedirectToAction("CategoryList");
        }

        public ActionResult DeleteCats(string ids)
        {
            ids.Trim(',');
            BLLFactory<Category>.Instance.DeleteByCondition("CategoryID in (" + ids + ")");
            return RedirectToAction("CategoryList");
        }
        #endregion
    }
}
